<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" draggable>
  <el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" :label-width="100">
  <el-form-item label="景区名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="景区名称"></el-input>
  </el-form-item>
  <el-form-item label="手机号" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="手机号"></el-input>
  </el-form-item>
  <el-form-item label="负责人" prop="principal">
      <el-input v-model="dataForm.principal" placeholder="负责人"></el-input>
  </el-form-item>
  <el-form-item label="省份" prop="province">
      <el-input v-model="dataForm.province" placeholder="省份"></el-input>
  </el-form-item>
  <el-form-item label="城市" prop="city">
      <el-input v-model="dataForm.city" placeholder="城市"></el-input>
  </el-form-item>
  <el-form-item label="区域" prop="area">
      <el-input v-model="dataForm.area" placeholder="区域"></el-input>
  </el-form-item>
  <el-form-item label="具体地址" prop="address">
      <el-input v-model="dataForm.address" placeholder="具体地址"></el-input>
  </el-form-item>
  <el-form-item label="是否运营" prop="isRun">
      <el-input v-model="dataForm.isRun" placeholder="是否运营"></el-input>
  </el-form-item>
  </el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="submitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import { useOrderManagementApi, useOrderManagementSubmitApi } from '@/api/order/management'

const emit = defineEmits(['refreshDataList'])

const visible = defineModel<boolean>('visible')
const dataFormRef = ref()

const dataForm = reactive({
	id: '',
	createTime: '',
	updateTime: '',
	deleted: '',
	updater: '',
	name: '',
	phone: '',
	principal: '',
	province: '',
	city: '',
	area: '',
	address: '',
	isRun: '',
	creator: ''})

const init = (id?: number) => {
	if (id) {
		getManagement(id)
	}
}

const getManagement = (id: number) => {
	useOrderManagementApi(id).then(res => {
		Object.assign(dataForm, res.data)
	})
}

const dataRules = ref({
	name: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	phone: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	principal: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	province: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	city: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	area: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	address: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	isRun: [{ required: true, message: '必填项不能为空', trigger: 'blur' }]})

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate(async (valid: boolean) => {
		if (!valid) {
			return false
		}


		useOrderManagementSubmitApi(dataForm).then(() => {
			ElMessage.success({
				message: '操作成功',
				duration: 500,
				onClose: () => {
					visible.value = false
					emit('refreshDataList')
				}
			})
		})
	})
}

defineExpose({
	init
})
</script>
